define(function(require){
	var main = require('/static/main.js');
	require('/static/ajaxfileupload.js');
	var m = new main();
	function Cont(){
		this.Fun._init();
	}
	Cont.prototype.Fun = {
		_init:function(){
			var _this = this;
		    this.getdetial();
		    this.changeHead();
			this.bindEvent();
		},
		getdetial:function(){
			$.ajax({
                type: "POST",
                url: "/userinfo/getinfo",
                dataType: "json",
                success: function (ret) {
                	if(ret.IsBizSuccess){
                		var ret = ret.Model;
                		$('.UserName').val(ret.Nickname)
                		$('.img-circle').attr('src',ret.Photo)
                		$('.InvitationCode').html(ret.InvitationCode);
                		$('.LevelName').html(ret.LevelName);
                	}else{
                		layer.msg('请先登录！');
                		setTimeout(function(){
                			window.location.href='/login/index';
                		},700)
                		return;
                	}
                }
            });
		},
		editinfo:function(){
			var data = {
				Nickname:$('.UserName').val(),
				Photo:$('.img-circle').attr('data-src')
			}
			$.ajax({
                type: "POST",
                url: "/userinfo/editinfo",
                data:data,
                dataType: "json",
                success: function (ret) {
                	if(ret.IsBizSuccess){
                		layer.msg('修改成功！');
                		return false;
                	}else{
                		layer.msg(ret.ErrMsg);
                		return false;
                	}
                }
            });
		},
		changeHead:function(){
            var _this = this;
            //图片上传
            $("#file").on("change",function () {
                ajaxFileUpload();
            });
            function ajaxFileUpload(){
                $.ajaxFileUpload({
                    url:m.baseUrl+"/customer/edit-head",
                    secureuri: false,
                    fileElementId: 'file',
                    dataType: 'json',
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            $(".img-circle").attr({"src":ret.Photo,'data-src':ret.FileName})
                        }else{
                        	layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                })
                return false;
            }
        },
		bindEvent:function(){
			var _this = this;
			// 保存修改信息
			$('.userinfo_edit').click(function(){
		    	_this.editinfo();
		    })
		    // 设置登录密码
            $('.saveChage').click(function(){
            	var $this = $(this);
            	var isclick = parseInt($(this).attr('data-isclick'));
            	if(isclick == 1){
            		return false;
            	}
                var OldPassword = $('.OldPassword').val();
                var NewPassword = $('.NewPassword').val();
                var NewPasswordAgin = $('.NewPasswordAgin').val();

                if(OldPassword == '' || NewPassword == '' || NewPasswordAgin == ''){
                	layer.msg('密码不能为空');
                    return false;
                }
                if(NewPassword.length < 4 || NewPassword.length > 16){
                    if(NewPassword.length < 4){
                        layer.msg('密码不能小于4位！');
                    }
                    if(NewPassword.length > 16){
                        layer.msg('密码不能大于16位！');
                    }
                    return false;
                }

                if($.trim(NewPassword) != $.trim(NewPasswordAgin)){
                    layer.msg('两次密码不一致请重新输入');
                    return false;
                }
                var data = {
                	OldPassword:OldPassword,
                	NewPassword:NewPassword,
                }
                $.ajax({
	                type: "POST",
	                url: "/userinfo/change-pwd",
	                data:data,
	                dataType: "json",
                    success:function(ret){
                        if(ret.IsBizSuccess){
                    		$this.attr('data-isclick',1)
                    		layer.msg('修改成功！');
                            setTimeout(function (){
                                window.location.href = '/login/index';
                            }, 1000);
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                })
            })



		}


	}
	var a = new Cont();

})